from kubernetes import client, config
import utils


def cpu_format_to_float(cpu_data):
    cpu_string = cpu_data[:-1]
    return float(cpu_string)


def float_to_cpu_format(float_data):
    return str(float_data) + "m"


def mem_format_to_float(mem_data):
    mem_string = mem_data[:-2]
    return float(mem_string)


def float_to_mem_format(float_data):
    return str(float_data) + "Mi"


def trans_format(label, name, ipl, t, v):
    return label + ":" + name + ":" + ipl + ":" + t + ":" + v


def collect_k8s():
    config.load_kube_config()

    v1 = client.CoreV1Api()
    ret = v1.list_pod_for_all_namespaces(watch=False)
    labels = ["cluster_name"]
    resource_name = ["cpu", "memory"]
    msgs = []
    for i in ret.items:
        limit_cpu = 0
        limit_mem = 0
        request_cpu = 0
        request_mem = 0
        request_es = 0

        ip = None
        if i.status.host_ip:
            ip = i.status.host_ip
        else:
            ip = utils.get_network_ip()

        if i.metadata.cluster_name:
            msgs.append(trans_format(
                "clu_na", i.metadata.name, ip, utils.get_ms_time(), i.metadata.cluster_name))
        else:
            msgs.append(trans_format(
                "clu_na", i.metadata.name, ip, utils.get_ms_time(), "None"))

        for c in i.spec.containers:
            resource = c.resources
            limits = resource.limits
            requests = resource.requests
            if limits:
                if 'cpu' in limits:
                    limit_cpu = limit_cpu + cpu_format_to_float(limits['cpu'])
                if 'memory' in limits:
                    limit_mem = limit_mem + mem_format_to_float(limits['memory'])
            if requests:
                if 'cpu' in requests:
                    request_cpu = request_cpu + cpu_format_to_float(requests['cpu'])
                if 'memory' in requests:
                    request_mem = request_mem + mem_format_to_float(requests['memory'])
                if 'ephemeral-storage' in requests:
                    request_es = request_es + mem_format_to_float(requests['ephemeral-storage'])
        msgs.append(trans_format(
            "cpu_re", i.metadata.name, ip, utils.get_ms_time(), float_to_cpu_format(request_cpu)))
        msgs.append(trans_format(
            "mem_re", i.metadata.name, ip, utils.get_ms_time(), float_to_mem_format(request_mem)))
        msgs.append(trans_format(
            "cpu_li", i.metadata.name, ip, utils.get_ms_time(), float_to_cpu_format(limit_cpu)))
        msgs.append(trans_format(
            "mem_li", i.metadata.name, ip, utils.get_ms_time(), float_to_mem_format(limit_mem)))
        msgs.append(trans_format(
            "es_re", i.metadata.name, ip, utils.get_ms_time(), float_to_mem_format(request_es)))
    return msgs
    # redisPool = redis.ConnectionPool(host='localhost', port=6379, db=1, decode_responses=True)
    # rdb = redis.Redis(connection_pool=redisPool)
    # for msg in msgs:
    #     rdb.rpush(sys.argv[1], msg)
    # rdb.close()
